package com.datang.petstore.service.impl;


import com.datang.petstore.entity.Role;
import com.datang.petstore.entity.RoleExample;
import com.datang.petstore.generatemapper.RoleMapper;
import com.datang.petstore.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-11-01 11:44
 * @notify 用户角色接口
 * @version 1.0
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    /*
     * @author 顶风少年
     * @date 2019/11/1 11:47
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userId
     * @result java.util.List<com.datang.petstore.entity.Role>
     * @notify 根据用户id查询用户角色
     * version 1.0
     */
    @Cacheable(value = "getRoleByUserId", key = "#userId")
    public List<Role> getRoleByUserId(String userId) {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        return roleMapper.selectByExample(roleExample);
    }


    /*
     * @author 顶风少年
     * @date 2019/11/20 11:15
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result java.util.List<com.datang.petstore.entity.Role>
     * @notify 获取全部角色
     * version 1.0
     */
    @Cacheable(value = "getRoleList", key = "1")
    public List<Role> getRoleList(){
        List<Role> roles = roleMapper.selectByExample(null);
        return roles;
    }


    /*
     * @author 顶风少年
     * @date 2019/11/20 11:12
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param role
     * @result void
     * @notify 添加角色
     * version 1.0
     */
    @CacheEvict(value = "getRoleList", key = "1")
    public void addRole(Role role) {
        role.setDate(new Date());
        roleMapper.insert(role);
    }


    /*
     * @author 顶风少年
     * @date 2019/11/20 11:12
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param role
     * @result void
     * @notify 删除角色
     * version 1.0
     */
    @CacheEvict(value = "getRoleList", key = "1")
    public void deleteRole(Role role) {
        roleMapper.deleteByPrimaryKey(role.getId());
    }

}
